<?
/*-- Project Introduce --*/


/**
 * What's this Class ?
 *
 * @author		alee
 * @access		public
 */
class JJ_ExternalCommand
{
	/**
	 * 检查命令
	 *
	 * @access	public
	 * @static
	 * @return	void
	 */
	static public function CheckCommand($sCmdDir)
	{
		
		$arrCmdTypes = array('talkto','subscription') ;
		
		foreach ($arrCmdTypes as $sCmdType)
		{
			$sCmdTypeDir = $sCmdDir.$sCmdType.'/' ;
			if( !is_dir($sCmdTypeDir) )
			{
				continue ;
			}
			
			$aIterator = new JCAT_FSOIterator($sCmdTypeDir,JCAT_FSOIterator::FILE|JCAT_FSOIterator::RETURN_NAME) ;
			$aIterator->First() ;
			while( !$aIterator->IsDone() )
			{
				$sJID = $aIterator->Current() ;
				$sCmdDataTempFile = $sCmdTypeDir.$sJID ;
				
				call_user_func_array( array(__CLASS__,'Process'.$sCmdType), array($sCmdDataTempFile) ) ;
				unlink($sCmdDataTempFile) ;
				
				$aIterator->Next() ;
			}		
		}
	}
	
	/**
	 * Description
	 *
	 * @access	public
	 * @static
	 * @return	void
	 */
	static public function ProcessTalkto($sCmdDataTempFile)
	{
		JCAT_ASSERT::ASSERT_FILE($sCmdDataTempFile) ;

		$aTo = JJ_Account::CreateFromString(basename($sCmdDataTempFile)) ;
		if($aTo)
		{
			$aSessionWindow = JJ_SessionWindow::GetInstance($aTo,JJ_MainController::GetGlobalInstance()) ;
			$aSessionWindow->ShowView() ;
			
			$sSay = file_get_contents($sCmdDataTempFile) ;
			if($sSay)
			{
				$aSessionWindow->InsertWord($sSay) ;
			}
		}
	}
	
	/**
	 * Description
	 *
	 * @access	public
	 * @static
	 * @return	void
	 */
	static public function ProcessSubscription($sCmdDataTempFile)
	{
		JCAT_ASSERT::ASSERT_FILE($sCmdDataTempFile) ;

		$aTo = JJ_Account::CreateFromString(basename($sCmdDataTempFile)) ;
		if($aTo)
		{
			$sRequestWord = file_get_contents($sCmdDataTempFile) ;
			
			$theMainController = JJ_MainController::GetGlobalInstance() ;
			$theMainController->AddContact($aTo->GetJabberAddress(),$sRequestWord) ;
		}
	}
}

?>